package com.pzhu.dormitory.controller;


import com.pzhu.dormitory.entity.StudentEntity;
import com.pzhu.dormitory.service.StudentService;
import com.pzhu.dormitory.service.UserService;
import com.pzhu.dormitory.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@Api(tags = "视图", value = "负责返回视图")
@RequestMapping("/index")
@Controller
@AllArgsConstructor
public class IndexController {

    private final UserService userService;
    private final StudentService studentService;

    @ApiOperation(value = "跳转 404 页面")
    @GetMapping("/404")
    public String error404() {
        return "error/404";
    }

    @ApiOperation(value = "跳转 403 页面")
    @GetMapping("/403")
    public String error403() {
        return "error/403";
    }

    @ApiOperation(value = "跳转 500 页面")
    @GetMapping("/500")
    public String error500() {
        return "error/500";
    }

    @ApiOperation(value = "进入登录页面")
    @GetMapping(value = {"/login", "/"})
    public String login() {
        return "login";
    }


    @ApiOperation(value = "进入注册页面")
    @GetMapping(value = {"/register"})
    public String register() {
        return "register";
    }

    @ApiOperation(value = "进入home页面")
    @GetMapping("/home")
    public String home(Model model) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserVo userVo = userService.getUserInfoByUsername(authentication.getName());
        model.addAttribute("entity", userVo);
        return "home";
    }

    @ApiOperation(value = "进入 main 页面")
    @GetMapping("/main")
    public String main() {

        return "main";
    }


    @GetMapping("/users")
    @ApiOperation(value = "跳转用户管理页面")
    public String userList() {
        return "users/user";
    }


    @ApiOperation(value = "跳转个人用户信息编辑页面")
    @GetMapping("/user/updateSelfInfo")
    public String userDetail(Model model) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserVo userVo = userService.getUserInfoByUsername(authentication.getName());
        model.addAttribute("entity", userVo);
        return "users/user_edit";
    }

    @ApiOperation(value = "跳转个人用户编辑密码页面")
    @GetMapping("/user/editPassword")
    public String userPwd() {

        return "users/edit_password";
    }


    @ApiOperation(value = "跳转到角色管理页面")
    @GetMapping("/role")
    public String role() {
        return "role/role";
    }

    @ApiOperation(value = "跳转到菜单管理页面")
    @GetMapping("/menu")
    public String menu() {
        return "menu/menu";
    }


    @ApiOperation(value = "学生信息管理")
    @GetMapping("/student")
    public String student() {
        return "student/student";
    }


    @ApiOperation(value = "校区信息管理")
    @GetMapping("/campus")
    public String campus() {
        return "campus/campus";
    }

    @ApiOperation(value = "公寓楼管理")
    @GetMapping("/building")
    public String building(){
        return "building/building";
    }

    @ApiOperation(value = "寝室管理")
    @GetMapping("/room")
    public String room(){
        return "room/room";
    }

    @ApiOperation(value = "床位管理")
    @GetMapping("/position")
    public String position(){
        return "position/position";
    }

    @ApiOperation(value = "申请床位")
    @GetMapping("positionEdit")
    public String positionEdit(Model model){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserVo userVo = userService.getUserInfoByUsername(authentication.getName());
        StudentEntity one = studentService.lambdaQuery().eq(StudentEntity::getSno, userVo.getUsername()).one();
        model.addAttribute("entity", one);
        return "position/edit";
    }

    @ApiOperation(value = "查看学生信息")
    @GetMapping("/studentg")
    public String studentg() {
        return "student/studentg";
    }

    @ApiOperation(value = "缺勤记录")
    @GetMapping("/absentFromWorkg")
    public String absentFromWorkg() {
        return "absentFromWork/absentFromWorkg";
    }

    @ApiOperation(value = "缺勤记录")
    @GetMapping("/absentFromWork")
    public String absentFromWork() {
        return "absentFromWork/absentFromWork";
    }

    @ApiOperation(value = "访客管理")
    @GetMapping("/visit")
    public String visit() {
        return "visit/visit";
    }

    @ApiOperation(value = "公告管理")
    @GetMapping("/ad")
    public String ad() {
        return "ad/ad";
    }


    @ApiOperation(value = "查看公告")
    @GetMapping("/ads")
    public String ads() {
        return "ad/ads";
    }

    @ApiOperation(value = "电费重置")
    @GetMapping("/cost")
    public String cost(){
        return "cost/cost";
    }

    @ApiOperation(value = "网费重置")
    @GetMapping("/costw")
    public String costw(){
        return "cost/costw";
    }
}
